package com.cloudlead.customerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.customerAction.service.CustomerOrdeFindSer;
import com.cloudlead.customerService.vo.CreateCustmoerOrderVo;
import com.cloudlead.customerService.vo.CustmoerOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 123 on 2015/8/31.
 */
@Service
public class CustomerOrderFindSerImpl implements CustomerOrdeFindSer {

    @Autowired
    TbCustomerOrderService customerOrderService;
    @Autowired
    BaseCompanyService   baseCompanyService;
    @Autowired
    RegionService regionService;
    @Autowired
    BaseVoyageService baseVoyageService;

    @Autowired
    TbContainerInfoService tbContainerInfoService;

    @Autowired
    TbFleetOrderService tbFleetOrderService;

    @Override
    //通过下单公司的ID查询
    public Page<Object> CustomerOrderFind(CustmoerOrderVo vo, int size, int page) {
        PageRequest pr=new PageRequest();
        List<Term> tr=new ArrayList<>();
            tr.add(new Term("flag","0"));//未删除
        //查询条件
        String baseCompanyId=vo.getBaseCompany();
        if(baseCompanyId!=null&&(!baseCompanyId.equals(""))){
            tr.add(new Term("baseCompanyId",baseCompanyId));//下单公司ID
        }

        Filter fl=new Filter(new Criteria(tr));
            pr.setPage(page);//页码
            pr.setSize(size);//每页数量
            pr.setFilter(fl);
        Sort sort = new Sort();//订单创建时间倒序
            sort.add(new Sort.Order(Sort.Direction.DESC,"orderTime"));
            pr.setSort(sort);
        Page<TbCustomerOrder> pl=customerOrderService.findAll(pr);//查询出全部的值
        List<TbCustomerOrder> pll=pl.getContent();
        List<TbCustomerOrder> lpvo = new ArrayList<TbCustomerOrder>();
        for(Iterator<TbCustomerOrder> it = pll.iterator();it.hasNext();) {//遍历
            TbCustomerOrder tco = it.next();
            if(tco.getBaseCompany()!=null&&tco.getBaseCompany().getId()!=null) {//公司
                BaseCompany bc = baseCompanyService.view(tco.getBaseCompany().getId());
                tco.setBaseCompany(bc);
            }
            if(tco.getsDo()!=null&&tco.getsDo().getId()!=null) {
                Region re = regionService.view(tco.getsDo().getId());
                tco.setsDo(re);
            }
            if(tco.getfDo()!=null&&tco.getfDo().getId()!=null) {
                Region re = regionService.view(tco.getfDo().getId());
                tco.setfDo(re);
            }
        /*    if(tco.getBaseVoya()!=null&&tco.getBaseVoya().getId()!=null){
                BaseVoyage bv =baseVoyageService.view(tco.getBaseVoya().getId());
                tco.setBaseVoya(bv);
            }*/
            lpvo.add(tco);
        }
        pl.setContent(lpvo);
        Page<Object> page1=new Page<>();
        BeanUtils.copyProperties(pl,page1);//把pl的值赋给page1
        return page1;
    }


    @Override
    //通过ID查询
    public Object CustomerOrderFindId(String id) {
        TbCustomerOrder customerOrder=customerOrderService.view(id);//ID
        return customerOrder;
    }

    @Override
    public String updateQuantity(String id,int page) {
        List<TbCustomerOrder> tbCustomerOrderList = null;
        if (id != null && !"".equals(id)) {
            tbCustomerOrderList = customerOrderService.list(new Filter(new Criteria(new Term("id", id))), new Sort());

        } else {
            PageRequest pageRequest = new PageRequest();
            pageRequest.setPage(page);
           pageRequest.setSize(1000);
           //pageRequest.setFilter(new Filter(new Criteria(new Term("quantity", null))));
            tbCustomerOrderList = customerOrderService.findAll(pageRequest).getContent();
        }

        for (TbCustomerOrder tbCustomerOrder : tbCustomerOrderList ){
         //   if(tbCustomerOrder.getQuantity()==null||"".equals(tbCustomerOrder.getQuantity())) {
                List<TbContainerInfo> list=null;

                //起始港车队订单
                List<Term> fleetOrderTr = new ArrayList<Term>();//and 条件
                fleetOrderTr.add(new Term("tbCustomerOrder.id",tbCustomerOrder.getId()));

                if(tbCustomerOrder.getDestCompany()!=null){//进口
                    fleetOrderTr.add(new Term("isStart","0"));
                }else {
                    fleetOrderTr.add(new Term("isStart","1"));//出口
                }
                //获取车队订单集合
                List<TbFleetOrder> tbFleetOrderList=tbFleetOrderService.list(new Filter(new Criteria(fleetOrderTr)),new Sort());

                List fleetOrderListNew = new ArrayList();//保存车队订单ID
                for(TbFleetOrder fleetOrder: tbFleetOrderList){
                    fleetOrderListNew.add(fleetOrder.getId());
                }

                //按车队定Id查询箱集合
                list = tbContainerInfoService.list(new Filter(new Criteria(new Term("fleetOrder.id",Operator.IN,fleetOrderListNew))), new Sort());

                String[] typeList = new String[list.size()];

                for (int i = 0; i < list.size(); i++) {

                    typeList[i] = list.get(i).getType();//保存所有箱子的箱类型
                }
                tbCustomerOrder.setQuantity(theType(typeList));
                customerOrderService.update(tbCustomerOrder);//更新订单状态
          //  }
        }

        return "修改成功";

    }

    @Override
    public String updateQuantityByFleet(String id,int page) {
        List<TbFleetOrder> tbFleetOrderList = null;
        if (id != null && !"".equals(id)) {
            tbFleetOrderList = tbFleetOrderService.list(new Filter(new Criteria(new Term("id", id))), new Sort());

        } else {
            PageRequest pageRequest = new PageRequest();
              pageRequest.setPage(page);
              pageRequest.setSize(1000);
          //  pageRequest.setFilter(new Filter(new Criteria(new Term("quantity",null))));
            tbFleetOrderList = tbFleetOrderService.findAll(pageRequest).getContent();
        }

        for (TbFleetOrder tbFleetOrder : tbFleetOrderList ){
            if(tbFleetOrder.getQuantity() == null||"".equals(tbFleetOrder.getQuantity())) {
                List<TbContainerInfo> list = tbContainerInfoService.list(new Filter(new Criteria(new Term("fleetOrder.id", tbFleetOrder.getId()))), new Sort());
                String[] typeList = new String[list.size()];

                for (int i = 0; i < list.size(); i++) {

                    typeList[i] = list.get(i).getType();//保存所有箱子的箱类型
                }
                tbFleetOrder.setQuantity(theType(typeList));
                tbFleetOrderService.update(tbFleetOrder);//更新订单状态
            }
        }

        return "修改成功";
    }


    //统计箱类型
    public String getTypes(String[] typeList,String type){
        int count=0;
        for(int i=0;i<typeList.length;i++){

           if(type.equals(typeList[i])){
               count++;
           }
        }
        if(count>0){
            return count+"×"+type;
        }else{
            return null;
        }

    }

    //统计箱类型
    public String theType(String[] typeList){
        //统计箱类型
        String gp20=getTypes(typeList,"20GP");
        String gp40=getTypes(typeList, "40GP");
        String hc40=getTypes(typeList, "40HC");
        String hc20=getTypes(typeList, "20HC");
        String tk40=getTypes(typeList, "40TK");
        String tk20=getTypes(typeList, "20TK");
        String rh40=getTypes(typeList, "40RH");
        String rh20=getTypes(typeList, "20RH");
        //累加箱类型
        String str="";
        str+=gp20!=null?gp20+",":"";
        str+=gp40!=null?gp40+",":"";
        str+=hc40!=null?hc40+",":"";
        str+=hc20!=null?hc20+",":"";
        str+=tk40!=null?tk40+",":"";
        str+=tk20!=null?tk20+",":"";
        str+=rh40!=null?rh40+",":"";
        str+=rh20!=null?rh20+",":"";
        if(str.length()>0){
            str=str.substring(0,str.length()-1);
        }
        return str;
    }


}
